package com.hsxc.cdz.monitor.FSM;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.bean.Reserv;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.FSM.PileMemState.Status;
import com.hsxc.cdz.monitor.executor.ServerExecutor;
import com.hsxc.cdz.monitor.executor.ServerExecutorNew;
import com.hsxc.cdz.trans.bill.CacheTrade;
import com.hsxc.cdz.trans.bill.CacheTrade.TradeStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 作者: 陈钊
 * @version 创建时间：2016年6月23日 下午3:16:39 类说明 ：
 */
public class PileMemStateLocked extends PileMemState {

    private Logger logger = LoggerFactory.getLogger(PileMemStateLocked.class);
    public PileMemStateLocked(CachePile pile) {
        // TODO Auto-generated constructor stub
        this.status = Status.PILE_STATUS_LOCKED;
        this.pile = pile;
    }

    @Override
    int fault() {

        this.status = Status.PILE_STATUS_FAULT;
        this.pile.state = new PileMemStateFault(this.pile);

        // 更新DB
        updatePileStatus(this.pile);
        // 更新缓存
        GlobalManager.pileRegister.delPileManually(this.pile.getAddress());
        return 0;
    }

    @Override
    int recovery() {
        return 0;
    }

    @Override
    int lock() {
        return CachePile.PILE_STATUS_LOCKED;
    }

    @Override
    int unlock() {
        this.pile.state = new PileMemStateIdle(this.pile);
        // 更新数据库
        updatePileStatus(pile);
        return 0;
    }

    @Override
    int heartBeatLost() {
        return 0;
    }

    @Override
    public int logout() {
        this.pile.state = new PileMemStateIdle(this.pile);
        updatePileStatus(this.pile);
        GlobalManager.pileManager.delPile(this.pile.getAddress());
        return 0;
    }

    @Override
    public int reboot() {
        ServerExecutor executor = new ServerExecutor();
        int i = executor.restart(this.pile.getAddress(), this.pile.getProtocolVersion(), this.pile.getPileType());
        if (i == 0) {
            return 0;
        }
        return 1;
    }

    @Override
    public boolean confirmStart(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
        // 先修改交易、账单状态等信息
       logger.info("TRANS:Start to confirm charge start of pile " + pileNo + " user ." + userNo);
        // String newUserNo = userNo.substring(2)+"00";

        CacheTrade cacheTrade = GlobalManager.tradeManager.getTrade(pileNo, userNo);
        if (cacheTrade == null) {
            logger.info("TRANS:Can't find trade for user " + userNo + " pile " + pileNo);
            return true;
        }
        GlobalManager.tradeManager.updateTrade(cacheTrade.getTid(), TradeStatus.TRADE_STATUS_ON, 0, 0);
        Bill bill = new Bill();
        bill.setBillNO(cacheTrade.getTid());
        bill.setPayWay(cacheTrade.getType());
        bill.setPileNO(pileNo);
        bill.setUserNO(userNo);
        bill.setState(Bill.CHARGING);
        bill.setStartTime(cacheTrade.getStartTime());
        bill.setGenerateTime(TimeFormatter.toTimeYear());
        //设置预约信息
        bill.setResNO(cacheTrade.getReservNo());
        GlobalManager.billManager.createBill(bill);
        logger.info("TRANS:-------创建账单OK-------pile: " + pileNo);

        this.pile.state = new PileMemStateCharging(this.pile);
        if (updatePileStatus(this.pile) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public int chargeStart_jiaoliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
                                   String startTime, String cardNum, String pileAddress, String operatorNO, String protocol, int pileType) {
        return ServerExecutor.startCharge_jiaoliu(chargePort, startWay, ctrlWay, stopWay, limitData, startTime, cardNum,
                pileAddress, operatorNO, protocol, pileType);
    }

    @Override
    public int chargeStart_zhiliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
                                  String startTime, String cardNum, String pileAddress, String operatorNO, int chargeDataInterval,
                                  int batteryDataInteval, String protocol, int pileType) {
        return ServerExecutorNew.startCharge_zhiliu(chargePort, startWay, ctrlWay, stopWay, limitData, startTime,
                cardNum, pileAddress, operatorNO, chargeDataInterval, batteryDataInteval, protocol, pileType);
    }

    @Override
    public int chargeStop(int chargePort, int control, String userNO, String protocol, int pileType) {
        return 1;
    }

    @Override
    public boolean confirmStop(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
        return false;
    }

    @Override
    int reserve(int chargePort, String cardNo, int duration, String pileAddress, String operatorNo, String protocol,
                int pileType) {
        return 1;
    }

    @Override
    int confirmReserv_jiaoliu(int chargePort, String cardNo, String pileAddress, String operator, String protocol,
                              int pileType) {
        return 1;
    }

    @Override
    int confirmReserv_zhiliu(int chargePort, String cardNo, String resStartTime, String pileAddress, String operator,
                             String protocol, int pileType) {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    int reservationCancel(int chargePort, String cardNum, String pileadress, String operatorNo, String protocol,
                          int pileType) {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    int confirmReservCancelled(int chargePort, String cardNo, String pileAddress, String operatorNo, String protocol,
                               int pileType) {
        // TODO Auto-generated method stub
        return 1;
    }

}
